#include "Game.h"

/////////////////////////////////////
// Constructors / Destructors      //
/////////////////////////////////////
CGame::CGame()
{
	this->m_dwCreationWidth    = 800;
	this->m_dwCreationHeight   = 600;	
	this->m_strWindowTitle	   = "Pacman";
	this->m_hIcon			   = ::LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_DIRECTX));
	m_MenuOrGame			   = 0;
	this->m_Points			   = 0;
	this->m_Lives			   = 0;
	this->bonusTime			   = 0;
}

CGame::~CGame()
{
}

/////////////////////////////////////
// Memberfuncties                  //
/////////////////////////////////////
HRESULT CGame::GameInit()
{
	HRESULT res;
	
	//COM Object maken
	zen.m_pD3D = Direct3DCreate8(D3D_SDK_VERSION);
	if (!zen.m_pD3D)
	{
		::MessageBox(NULL, "Geen DirectX Interface!", "Error", 0);
		return E_FAIL;
	}
	
	//Aanmaken van een DirectX device
	res = zen.InitDirect3DDevice( m_hWnd, m_dwCreationWidth, m_dwCreationHeight, 
								  TRUE, D3DFMT_X8R8G8B8, zen.m_pD3D, &zen.m_pDevice );
	if (FAILED(res))
	{
		::MessageBox(NULL, "Geen DirectX device!", "Error", 0);
		return E_FAIL;
	}

	//Ophalen van een backbuffer-pointer 
	res = zen.m_pDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &zen.m_pBackSurface);
	if (FAILED(res))
		zen.SetError("Kan geen backbuffer vinden!");

	zen.LoadBitmapToSurface("res/ex1Background.bmp", &m_pBackgroundSurface, zen.m_pDevice);
	zen.LoadBitmapToSurface("res/background.bmp", &m_pStarBackground, zen.m_pDevice);
	zen.LoadBitmapToSurface("res/levelcompleted.bmp", &m_pLevelCompleted, zen.m_pDevice);
	zen.LoadBitmapToSurface("res/youloose.bmp", &m_pYouLose, zen.m_pDevice);	
	zen.LoadBitmapToSurface("res/youbeat.bmp", &m_pYouBeat, zen.m_pDevice);	
	
	// Intialize timing for frame rate counters, etc
	srand( GetTickCount() );	
	res = zen.InitTiming();
		if (FAILED(res))
	{
		::MessageBox(NULL, "timing", "Error", 0);
		return E_FAIL;
	}
	
	// Keyboard
	res = zen.InitializeInput(m_hInstance);
	if (FAILED(res))
	{
		::MessageBox(NULL, "Can't initialize input.", "Error", 0);
		return E_FAIL;
	}

	res = m_Mouse.Initialize(m_hWnd);
	if (FAILED(res))
	{
		::MessageBox(NULL, "Can't initialize mouse", "Error", 0);
		return E_FAIL;
	}

	res = m_Keyboard.Initialize(m_hWnd);
	//zen.InitializeInput(m_hInstance);

	if (FAILED(res))
	{
		::MessageBox(NULL, "Can't initialize keyboard.", "Error", 0);
		return E_FAIL;
	}

	//m_Sound = CZenSound();
	res = m_Sound.InitSound(m_hWnd);
	if (FAILED(res))
	{
		::MessageBox(NULL, "Can't initialize sound.", "Error", 0);
		return E_FAIL;
	}
	m_Sound.SetMusicOnBool(&menu.m_bSoundOnOption);
	m_Sound.SetSoundOnBool(&menu.m_bMusicOnOption);

	m_Sound.PlayBackgroundMusic("res/soundeffects/pacman_intro.wav");
		
	//Menu
	menu.Init(&zen,&m_Mouse, &m_Sound, this->m_dwCreationHeight, this->m_dwCreationWidth);	
	prevTime = GetTickCount();
	wolfenstein3d=false;
	return S_OK;
}

HRESULT CGame::Render()
{	
	D3DLOCKED_RECT	LockedRect;
	HRESULT			res;

	zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
	
	if(m_MenuOrGame!=0)
	{
		zen.CopySurfaceToSurface(0, m_pStarBackground, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));
	}

	zen.m_pDevice->BeginScene();

	//rendering either game or menu
	if(m_MenuOrGame==0)
		menu.Render();
	else
	{
		scene.Render();		
	}

	zen.m_pDevice->EndScene();

	res = zen.m_pBackSurface->LockRect(&LockedRect, NULL, 0);
	if (FAILED(res))
	{
		zen.SetError("Kan geen lock krijgen op backbuffer!");
		return E_FAIL;
	}
	//Pointer naar scherm-pixels
	zen.PrintFrameRate( this->m_dwCreationWidth-60, this->m_dwCreationHeight-30, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );
	if(m_MenuOrGame!=0)
	{
		char stringpoints[10];
		char stringlives[10];
		char stringpointsText[] =  {'p','o','i','n','t','s',':','\0'};
		char stringlivesText[] =  {'l','i','v','e','s',':','\0'};
		ZeroMemory( &stringpoints, sizeof( stringpoints ) );
		itoa(this->m_Points, stringpoints, 10 );
		//itoa(scene.m_GameFieldFrame.m_numberFood, stringpoints, 10 );
		ZeroMemory( &stringlives, sizeof( stringlives ) );
		itoa(this->m_Lives, stringlives, 10 );
		zen.PrintString( 150, 10, stringpointsText, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );
		zen.PrintString( 260, 10, stringpoints, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );		
		zen.PrintString( 10, 10, stringlivesText, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );			
		zen.PrintString( 110, 10, stringlives, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );
		if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true)
		{
			if(this->bonusTime < 0)
			{
				scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
				this->bonusTime = 0;
				string line;
				string songLocation = "res/soundeffects/";
				ostringstream oss;
				oss << songLocation << m_CurrentLevel;
				songLocation = oss.str();
				songLocation += ".mid";
				m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
			}
			else
			{
				this->bonusTime--;
				char stringbonus[10];
				char stringbonusText[] =  {'b','o','n','u','s',':','\0'};
				ZeroMemory( &stringbonus, sizeof( stringbonus ) );
				itoa(((this->bonusTime/30)+1), stringbonus, 10 );	
				zen.PrintString( this->m_dwCreationWidth-160, 10, stringbonusText, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );					
				zen.PrintString( this->m_dwCreationWidth-60, 10, stringbonus, true, D3DCOLOR_ARGB( 0, 255, 0, 255 ), (DWORD*)LockedRect.pBits, LockedRect.Pitch );
			}
		}
	}

	//Geef lock op backbuffer vrij;
	zen.m_pBackSurface->UnlockRect();	

	zen.m_pDevice->Present( NULL, NULL, NULL, NULL );

	return S_OK;
}

HRESULT CGame::GameLoop()
{
	HRESULT res =m_Mouse.Poll();
	if(m_MenuOrGame==0)
		{
		
		menu.HandleMouseMove();
		if(menu.HandleMouseDown(&m_MenuOrGame))
		{			
			HRESULT res;
			res = StartLevel(1);
			this->m_Lives = 3;
			if (FAILED(res))
			{
				m_MenuOrGame = 0;
			}
		}
	}
	else if(!scene.m_GameFieldFrame.m_InitLight.animation)
	{	
		
		scene.HandleKeyInput(&m_Keyboard);
		float temp_x, temp_y, temp_z;
		if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true)
		{
			scene.m_GameFieldFrame.m_pacman.m_Frame2.GetPosition(temp_x, temp_z, temp_y);
			scene.m_GameFieldFrame.m_pacman.SetPosition(temp_x, temp_z, temp_y);
		}
		else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
		{
			scene.m_GameFieldFrame.m_pacman.m_Frame1.GetPosition(temp_x, temp_z, temp_y);
			scene.m_GameFieldFrame.m_pacman.SetPosition(temp_x, temp_z, temp_y);
		}
	
		if(wolfenstein3d)
		{
			scene.m_GameFieldFrame.wolfenstein3dMode=true;
			
			if(scene.direction==1)
			{	
				g_Camera.SetPosition( temp_x, 0.6, temp_y-0.5);
				g_Camera.SetLookPoint(temp_x,0.5f,temp_y +10);
			}
			else if(scene.direction==2)
			{
				g_Camera.SetPosition( temp_x, 0.6, temp_y+0.5);
				g_Camera.SetLookPoint(temp_x,1,temp_y-10);
			}
			else if(scene.direction==3)
			{
				g_Camera.SetPosition( temp_x+0.5, 0.6, temp_y);
				g_Camera.SetLookPoint(temp_x-10,0.6,temp_y);
			}
			else if(scene.direction==4)
			{
				g_Camera.SetPosition( temp_x-0.5, 0.6, temp_y);
				g_Camera.SetLookPoint(temp_x+10,0.6,temp_y);
			}
			g_Camera.Update();	
		}
		else if(changeCamera)
		{
			scene.m_GameFieldFrame.wolfenstein3dMode=false;
			g_Camera.SetPosition( scene.GetLength()/2.0-0.5, scene.GetLength()*1.2, 0 );
			g_Camera.SetLookPoint(scene.GetLength()/2.0f-0.5,0.0f,scene.GetWidth()/2.0f);
			g_Camera.Update();	
		}

		if(scene.direction==1)//UP
		{
			//check if we hit a ghost
			if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true)
				{
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
				}
				m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
					{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x+0.1)))
						{
							scene.m_GameFieldFrame.ghosts.at(i)->Reset();
						}
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x + 0.9)))
						{
							scene.m_GameFieldFrame.ghosts.at(i)->Reset();
						}
					}					
				}
				//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y ))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						}					
					}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x )))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						}					
					}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x + 1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x + 1))==2)
			{	
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x+1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x + 1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						}
					}
			}	
			//check if we hit a wall
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x+0.1))==1 || scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x + 0.9))==1)
			{		
				scene.m_GameFieldFrame.m_pacman.Move(0);
			}
			//check if we go to food
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 0)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/EAT.WAV") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/EAT.WAV");
				}
				m_Sound.PlayZenSound();
				//add point
				this->m_Points++;
				scene.m_GameFieldFrame.m_numberFood--;
				//eat ball				
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				//update pacman map position
				scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.5))=5;
				//update view
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y));
				//scene.m_GameFieldFrame.SetMap();				
				//check if there are balls left
				if(scene.m_GameFieldFrame.m_numberFood == 0)
				{
				m_Sound.LoadZenSound("res/soundeffects/yahoo.wav");
				m_Sound.PlayZenSound();
					//::MessageBox(NULL, "You WIN!!!", "Victory", 0);
					scene.m_GameFieldFrame.m_pacman.Move(0);
					scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
					this->bonusTime = 0;
					zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
					zen.CopySurfaceToSurface(0, m_pLevelCompleted, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));	
					zen.m_pDevice->Present( NULL, NULL, NULL, NULL );
					Sleep ( 1800);
					HRESULT res;
					res = StartLevel(m_CurrentLevel + 1);
					if (FAILED(res))
					{
						PostQuitMessage(0);
					}	
				}
			}
			//check if we go to a portal
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 4)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.5))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y));
				scene.m_GameFieldFrame.m_pacman.SetPosition(temp_x, 0.0f, 2);
				//scene.m_GameFieldFrame.SetMap();
			}
			//check if we go to empty field
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 5)
			{
				//eat ball				
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				//update pacman map position
				scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.5))=5;
				//update view
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y));
				//scene.m_GameFieldFrame.SetMap();
			}
			//check if we go to cherry bonus
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 6)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				//double points
				this->m_Points += this->m_Points;
				//eat ball				
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				//update pacman map position
				scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.5))=5;
				//update view
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y));
				//scene.m_GameFieldFrame.SetMap();
			}	
			//check if we go to eat ghost bonus
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 7)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				//change background sound
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					string songLocation = "res/soundeffects/eatghostbonus.wav";
					m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
				}
				//eat ball				
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				//update pacman map position
				scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.5))=5;
				//change pacman
				this->bonusTime	+= 900;
				scene.m_GameFieldFrame.m_pacman.m_eatGhost = true;
				//update view
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y));
				//scene.m_GameFieldFrame.SetMap();
			}
		}
		if(scene.direction==2)//DOWN
		{
			if(scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x + 0.9))==2)
			{
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
						for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
							POINT pos;
							pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
							if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
							if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						}					
					}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
					{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y ))&& (pos.x==(int)(temp_x+0.1)))
						{
							scene.m_GameFieldFrame.ghosts.at(i)->Reset();
						}
						if((pos.y==(int)(temp_y))&& (pos.x==(int)(temp_x + 0.9)))
						{
							scene.m_GameFieldFrame.ghosts.at(i)->Reset();
						}
					}					
				}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x )))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x + 1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x + 1))==2)
			{	
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x+1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x + 1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					}
					//eat ghost
			}	
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x+0.1))==1 || scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.9))==1)
			{		
				scene.m_GameFieldFrame.m_pacman.Move(0);
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 4)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.8)).at((int)(temp_x + 0.5))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.8));
				scene.m_GameFieldFrame.m_pacman.SetPosition(temp_x, 0.0f, scene.m_GameFieldFrame.GetWidth()-2);
			}			
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 0)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/EAT.WAV") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/EAT.WAV");
				}
				m_Sound.PlayZenSound();
				this->m_Points++;
				scene.m_GameFieldFrame.m_numberFood--;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.8)).at((int)(temp_x + 0.5))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.8));
				//scene.m_GameFieldFrame.SetMap();	
				if(scene.m_GameFieldFrame.m_numberFood == 0)
				{
				m_Sound.LoadZenSound("res/soundeffects/yahoo.wav");
				m_Sound.PlayZenSound();
					//::MessageBox(NULL, "You WIN!!!", "Victory", 0);
					scene.m_GameFieldFrame.m_pacman.Move(0);
					scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
					this->bonusTime = 0;

					zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
					zen.CopySurfaceToSurface(0, m_pLevelCompleted, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));
					zen.m_pDevice->Present( NULL, NULL, NULL, NULL );
					Sleep ( 1800);

					HRESULT res;
					res = StartLevel(m_CurrentLevel + 1);
					if (FAILED(res))
					{
						PostQuitMessage(0);
					}	
				}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 5)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.8)).at((int)(temp_x + 0.5))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.8));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 6)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				this->m_Points += this->m_Points;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.8)).at((int)(temp_x + 0.5))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.8));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 7)
			{			
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					string songLocation = "res/soundeffects/eatghostbonus.wav";
					m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
				}
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.8)).at((int)(temp_x + 0.5))=5;
				this->bonusTime	+= 900;
				scene.m_GameFieldFrame.m_pacman.m_eatGhost = true;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.8));
				//scene.m_GameFieldFrame.SetMap();
			}
		}
		if(scene.direction==3)//LEFT)
		{
			if(scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
						for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y ))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x )))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x + 1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x + 1))==2)
			{	
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x+1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x + 1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					
					}
					//eat ghost
			}	
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x))==1 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x))==1)
			{		
				scene.m_GameFieldFrame.m_pacman.Move(0);
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 4)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.8),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.m_pacman.SetPosition(scene.m_GameFieldFrame.GetLength()-3, 0.0f, temp_y);
			}			
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 0)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/EAT.WAV") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/EAT.WAV");
				}
				m_Sound.PlayZenSound();
				this->m_Points++;
				scene.m_GameFieldFrame.m_numberFood--;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.8),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();	
				if(scene.m_GameFieldFrame.m_numberFood == 0)
				{
				m_Sound.LoadZenSound("res/soundeffects/yahoo.wav");
				m_Sound.PlayZenSound();
				//	::MessageBox(NULL, "You WIN!!!", "Victory", 0);
					scene.m_GameFieldFrame.m_pacman.Move(0);
					scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
					this->bonusTime = 0;
					HRESULT res;

				zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
				zen.CopySurfaceToSurface(0, m_pLevelCompleted, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));	
				zen.m_pDevice->Present( NULL, NULL, NULL, NULL );
				Sleep ( 1800);


					res = StartLevel(m_CurrentLevel + 1);
					if (FAILED(res))
					{
						this->setHighScore();
						PostQuitMessage(0);
					}	
				}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 5)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.8),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 6)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				this->m_Points += this->m_Points;
				if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))!=4)
				{
					scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				}
				if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))!=4)
				{
					scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))=5;
				}
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.8),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 7)
			{			
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					string songLocation = "res/soundeffects/eatghostbonus.wav";
					m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
				}
				if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))!=4)
				{
					scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				}
				if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))!=4)
				{
					scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.8))=5;
				}
				this->bonusTime	+= 900;
				scene.m_GameFieldFrame.m_pacman.m_eatGhost = true;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.8),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
		}
		if(scene.direction==4)//RIGHT)
		{
			if(scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 1)).at((int)(temp_x + 0.9))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
						for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 1))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x+0.1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y)).at((int)(temp_x + 0.9))==2)
			{		

				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y ))&& (pos.x==(int)(temp_x+0.1)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y))&& (pos.x==(int)(temp_x + 0.9)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x))==2)
			{		
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x)))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x )))
							{
								scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					
					}
					//eat ghost
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x + 1))==2 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x + 1))==2)
			{	
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					this->PacmanDies(temp_x, temp_y);
					scene.m_GameFieldFrame.ResetGhosts();
				}
				else if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == true){
					if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/eat ghost.wav") == 0))
					{
						m_Sound.LoadZenSound("res/soundeffects/eat ghost.wav");
					}
					m_Sound.PlayZenSound();
					for(int i =0; i<scene.m_GameFieldFrame.ghosts.size();i++)
						{
						POINT pos;
						pos = scene.m_GameFieldFrame.ghosts.at(i)->GetIntPosition();
						if((pos.y==(int)(temp_y + 0.1))&& (pos.x==(int)(temp_x+1)))
							{
	scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}
						if((pos.y==(int)(temp_y + 0.9))&& (pos.x==(int)(temp_x + 1)))
							{
	scene.m_GameFieldFrame.ghosts.at(i)->Reset();
							}

						}
					}
					//eat ghost
			}	
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.1)).at((int)(temp_x + 1))==1 || scene.m_GameFieldFrame.map.at((int)(temp_y + 0.9)).at((int)(temp_x + 1))==1)
			{		
				scene.m_GameFieldFrame.m_pacman.Move(0);
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 4)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.m_pacman.SetPosition(2.0f, 0.0f, temp_y);
			}			
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 0)
			{
				//SOUND
				if(m_Sound.getLoadedSound()==NULL)
					::MessageBox(NULL, "Geen DirectX device!", "Error", 0);
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/EAT.WAV") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/EAT.WAV");
				}
				m_Sound.PlayZenSound();
				this->m_Points++;
				scene.m_GameFieldFrame.m_numberFood--;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();	
				if(scene.m_GameFieldFrame.m_numberFood == 0)
				{
					//::MessageBox(NULL, "You WIN!!!", "Victory", 0);
					scene.m_GameFieldFrame.m_pacman.Move(0);
				m_Sound.LoadZenSound("res/soundeffects/yahoo.wav");
				m_Sound.PlayZenSound();


					scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
					this->bonusTime = 0;
					zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
				zen.CopySurfaceToSurface(0, m_pLevelCompleted, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));
				zen.m_pDevice->Present( NULL, NULL, NULL, NULL );
				Sleep ( 1800);


					HRESULT res;
					res = StartLevel(m_CurrentLevel + 1);
					if (FAILED(res))
					{
						this->setHighScore();
						PostQuitMessage(0);
					}	
				}
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 5)
			{
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 6)
			{
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				this->m_Points += this->m_Points;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x))=5;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
			else if(scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5)) == 7)
			{			
				//SOUND
				if (!(strcmp(m_Sound.getLoadedSound(),"res/soundeffect/take_bonus.wav") == 0))
				{
					m_Sound.LoadZenSound("res/soundeffects/take_bonus.wav");
				}
				m_Sound.PlayZenSound();
				if(scene.m_GameFieldFrame.m_pacman.m_eatGhost == false)
				{
					string songLocation = "res/soundeffects/eatghostbonus.wav";
					m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
				}
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x + 0.5))=3;
				scene.m_GameFieldFrame.map.at((int)(temp_y + 0.5)).at((int)(temp_x))=5;
				this->bonusTime	+= 900;
				scene.m_GameFieldFrame.m_pacman.m_eatGhost = true;
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x + 0.5),(int)(temp_y + 0.5));
				scene.m_GameFieldFrame.SetMapAtPosition((int)(temp_x),(int)(temp_y + 0.5));
				//scene.m_GameFieldFrame.SetMap();
			}
		}
	}
	if(!scene.m_GameFieldFrame.m_InitLight.animation)
		scene.m_GameFieldFrame.GhostsAI();
			
	m_framesNumber=40;
	int time =0;
	while(1)
	{
		time=GetTickCount();
		if(((UINT)time-(UINT)prevTime)>(UINT)(1000/m_framesNumber))
			break;
	}
	prevTime=time;

	Render();
	zen.FrameCount();	
	return S_OK;
}

LRESULT CGame::MsgProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg) 
	{
		case WM_KEYDOWN:
		{
		//Menu
			if(m_MenuOrGame==0)
			{
				if(menu.HandleKeyInput(wParam, &m_MenuOrGame))
				{
					StartLevel(1);
					this->m_Lives = 3;
				}
				break;
			}
			else
			{
				switch( wParam )
				{			
					case VK_F1:
						if(!scene.m_GameFieldFrame.twoPlayerMode){

						wolfenstein3d=(!wolfenstein3d);
						if(wolfenstein3d==true)
							{
								float temp_x, temp_y, temp_z;
								scene.m_GameFieldFrame.m_pacman.m_Frame2.GetPosition(temp_x, temp_z, temp_y);
								g_Camera.SetPosition( temp_x, 0.6, temp_y-0.5);
								g_Camera.SetLookPoint(temp_x,0.5f,temp_y +10);
								g_Camera.Update();
							}
						changeCamera=true;
							}
						break;
					
					case VK_F2:
						scene.m_GameFieldFrame.macieksbool=(!scene.m_GameFieldFrame.macieksbool);
						break;

					case VK_F3:
						HRESULT res;
						scene.m_GameFieldFrame.m_pacman.Move(0);
						res = StartLevel(m_CurrentLevel + 1);						
						if (FAILED(res))
						{
							PostQuitMessage(0);
						}	
						break;

					case VK_F4:
						this->bonusTime	+= 900;
						scene.m_GameFieldFrame.m_pacman.m_eatGhost = true;
						break;

					case VK_F5:
						this->bonusTime	= 0;
						scene.m_GameFieldFrame.m_pacman.m_eatGhost = false;
						break;

					case VK_ESCAPE:
						PostQuitMessage(0);
						break;				
				}
				break;
			}
			break;
		}
	}
	return CWin::MsgProc(hWnd, uMsg, wParam, lParam);
}

HRESULT CGame::StartLevel(int numLevel)
{
	string line;
	string songLocation = "res/soundeffects/";
	ostringstream oss;
	oss << songLocation << numLevel;
	songLocation = oss.str();
	songLocation += ".mid";
	m_Sound.PlayBackgroundMusic((char *)songLocation.c_str());
	m_CurrentLevel = numLevel;
	
	if (m_CurrentLevel == 1)
	{
		zen.CreateViewport();
		zen.SetProjectionMatrix();

		zen.m_pDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
		
		zen.m_pDevice->SetVertexShader( ZENVERTEX_TYPE );
		zen.m_pDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
		
		zen.CreateViewport();
		zen.SetProjectionMatrix();
		zen.m_pDevice->SetRenderState( D3DRS_LIGHTING, TRUE);
		zen.m_pDevice->SetRenderState( D3DRS_AMBIENT, D3DCOLOR_XRGB( 0, 0, 0 ) );
	}

	HRESULT res;
	if(m_MenuOrGame ==1)scene.m_GameFieldFrame.twoPlayerMode=false;
	if(m_MenuOrGame ==2) scene.m_GameFieldFrame.twoPlayerMode = true;
	res = scene.Init(m_CurrentLevel, &m_Keyboard);
	if (FAILED(res))
	{
		return E_FAIL;
	}	

	g_Camera.SetPosition( scene.GetLength()/2.0-0.5, scene.GetLength()*1.3, 0 );
	g_Camera.SetLookPoint(scene.GetLength()/2.0f-0.5,0.0f,scene.GetWidth()/2.0f);
	g_Camera.Update();
			
	scene.m_GameFieldFrame.SetZenEngine(&zen);
	changeCamera=false;
	scene.m_GameFieldFrame.SetZenSound(&m_Sound);
	scene.m_GameFieldFrame.SetZenCamera(&g_Camera);
	/*if(wolfenstein3d==true)
		{
		Sleep(200);
		float temp_x, temp_y, temp_z;
		scene.m_GameFieldFrame.m_pacman.m_Frame2.GetPosition(temp_x, temp_z, temp_y);
		g_Camera.SetPosition( temp_x, 0.6, temp_y-0.5);
		g_Camera.SetLookPoint(temp_x,0.5f,temp_y +10);
		g_Camera.Update();
		}*/
	return S_OK;
}

void CGame::PacmanDies(float x, float y)
{
	//sound effect for dieing is played here.

		m_Sound.LoadZenSound("res/soundeffects/pacman_dies_y.wav");
		m_Sound.PlayZenSound();

	if(scene.m_GameFieldFrame.wolfenstein3dMode)
		scene.m_GameFieldFrame.wolfenstein3dMode = false;
	
	g_Camera.SetUp(0,1,0);
	g_Camera.SetPosition(x + 3, 8, y + 1);
	g_Camera.SetLookPoint(x, 0, y);
	g_Camera.Update();

	DWORD dwMilliseconds = 30;
	for (int i = 0; i < 100; i++)
	{
		scene.m_GameFieldFrame.m_pacman.SetPosition(x,(0.0f)-(i*0.01f), y);
		this->Render();
		scene.m_GameFieldFrame.GhostsAI();
		Sleep ( dwMilliseconds ); 
	}
	if(scene.m_GameFieldFrame.m_pacman.wolfenstein3dMode)
		scene.m_GameFieldFrame.wolfenstein3dMode = true;


	g_Camera.SetUp(0,1,0);
	g_Camera.SetPosition( scene.GetLength()/2.0-0.5, scene.GetLength()*1.2, 0 );
	g_Camera.SetLookPoint(scene.GetLength()/2.0f-0.5,0.0f,scene.GetWidth()/2.0f);
	g_Camera.Update();

	this->m_Lives--;

	scene.m_GameFieldFrame.m_pacman.SetPosition(scene.m_GameFieldFrame.m_StartX, 0.0f, scene.m_GameFieldFrame.m_StartY);
	scene.m_GameFieldFrame.m_pacman.Move(0);
	scene.m_GameFieldFrame.map.at(scene.m_GameFieldFrame.m_StartY).at(scene.m_GameFieldFrame.m_StartX)=3;
	
	if(wolfenstein3d==true)
							{
								float temp_x, temp_y, temp_z;
								scene.m_GameFieldFrame.m_pacman.m_Frame2.GetPosition(temp_x, temp_z, temp_y);
								g_Camera.SetPosition( temp_x, 0.6, temp_y-0.5);
								g_Camera.SetLookPoint(temp_x,0.5f,temp_y +10);
								g_Camera.Update();
							}
	if(this->m_Lives == 0)
	{
		//::MessageBox(NULL, "You LOSE!!!", "Defeat", 0);

			zen.m_pDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
			zen.CopySurfaceToSurface(0, m_pYouLose, 0, zen.m_pBackSurface, FALSE, D3DCOLOR_ARGB(0,0,0,0));
			//zen.m_pDevice->BeginScene();
			//zen.m_pBackSurface->UnlockRect();	
			zen.m_pDevice->Present( NULL, NULL, NULL, NULL );
			Sleep ( 5000);	
			setHighScore();
	
	PostQuitMessage(0);
	}

}

bool CGame::setHighScore()
{
	bool noScoreYet = true;
	int numbers[16];
	int number = 0;
	string line;
	string stringNumber;
	ifstream myfile ("highscores.txt");

	if (myfile.is_open())
	{
		while (! myfile.eof() && number < 8)
		{
			getline (myfile,line);
			char * temp = new char[line.length()+1];
			string tempNumber;
			strcpy(temp, line.c_str());
			for(unsigned int i = 0; i < line.length(); i++)
			{
				if (temp[i] == ',')
				{
					numbers[number*2] = atoi(tempNumber.c_str());
					tempNumber = "";
				}
				else
				{
					string buffer = "a";
					buffer[0] = line[i];
					tempNumber += buffer;
				}
			}
			if (m_Points > atoi(tempNumber.c_str()) && noScoreYet)
			{
				numbers[(number+1)*2] = numbers[number*2];
				numbers[((number+1)*2)+1] = atoi(tempNumber.c_str());

				numbers[number*2] = m_CurrentLevel;
				numbers[(number*2)+1] = m_Points;
				number++;
				number++;
				noScoreYet = false;
			}
			else
			{
				numbers[(number*2)+1] = atoi(tempNumber.c_str());
				number++;
			}
		}
	}
	myfile.close();

	ofstream myoutputfile ("highscores.txt");
	for (int i = 0; i < 16; i = i + 2)
	{	
		if (i == 14)
		{
			myoutputfile << numbers[i] << "," << numbers[i+1];
		}
		else
		{
			myoutputfile << numbers[i] << "," << numbers[i+1] << "\n";
		}
	}
	myoutputfile.close();

	return !noScoreYet;
}